import $ from 'jQuery';
(function () {
    /**
     * 选择公司
     * @private
     */


    let $vue = new Vue({
        el: '.create-account-hook',
        data: {
            roleList: [],
            constantList:[],
            account: {
                'companyName': '',
                'companyId': '',
                'email': '',
                'gender': 1,
                'mobile': '',
                'password': '',
                'roleId': '',
                'userName': ''
            },
            companyList: []
        },
        created: function () {
            M.Request.getRoleList(true, (roleList, ret) => {
               /* this.roleList = roleList;*/
                this.constantList= roleList;
            });
            M.Request.getCompanyList({}, true, (companyList, ret) => {
              /*  _selectCompany(companyList);/!**!/*/
                $.each(companyList,function(i,v){
                    $vue.$data.companyList.push({text:v.companyName,value:v.companyId,type:v.companyType})
                });
            });
        },
        methods: {
            goBack: function () {
                M.Page.emitPrePage();
                // M.Page.emitRefreshPage();
            },
            selectedCompany:function () {
                   if($vue.$data.account.companyId ==''){
                       $vue.$data.roleList.length =0;
                   }
                    $.each($vue.$data.companyList,function(i,v){
                        if ($vue.$data.account.companyId == v.value){
                            if(v.type == -1){
                                $vue.$data.roleList = $vue.$data.constantList.slice(0,3);
                            }else if(v.type ==1){
                                $vue.$data.roleList = $vue.$data.constantList.slice(3,4);
                            }else if(v.type ==4){
                                $vue.$data.roleList = $vue.$data.constantList.slice(4,5);
                            }
                            return false;
                        }
                    })
            }
        },
        mounted: function () {
            this.$nextTick(() => {
                let $platform = $('.create-account-hook');
                $platform.find('.form-hook').validate({
                    debug: true,
                    rules: {
                        roleId: {
                            required: true
                        },
                        companyId:{
                            required: true
                        },
                        mobile: {
                            required: true,
                            phoneCN: true,
                            minlength: 11,
                            maxlength: 11
                        },
                        email: {
                            email: true,
                            required: true,
                            minlength: 1,
                            maxlength: 50
                        },
                        userName: {
                            required: true,
                            minlength: 1,
                            maxlength: 10
                        },
                        password: {
                            required: true,
                            minlength: 8,
                            checkPwd: true,
                            maxlength: 20
                        },
                        confirmPassword: {
                            required: true,
                            equalTo: '#equalToPassowrd',
                        }
                    },
                    messages: {
                        password: {
                            required: '请输入密码',
                            minlength: '密码不能小于8位',
                            checkPwd: '(支持数字+字母+特殊符号)至少2种',
                            maxlength: '密码不能超过20位'
                        },
                        confirmPassword: {
                            equalTo: '两次密码输入不一致'
                        },
                    },
                    submitHandler: function (form) {
                        var reg4 = /^[a-zA-Z\u4e00-\u9fa5.]+$/;
                        if(reg4.test($.trim($vue.$data.account.userName)) == false){
                            layer.alert('用户名格式不正确，只允许输入汉字或英文字母');
                            return false;
                        }
                        let params = $vue.$data.account;
                        if( params.companyId==''){
                              layer.alert('请选择用户单位');
                            return false;
                        }
                        // console.log(JSON.stringify(params));
                        IOT.showOverlay('保存中，请稍等...');
                        IOT.getServerData(URI.ORGANIZATION.ACCOUNT.PLATFORM_CREATE_ACCOUNT.SAVE, params, function (ret) {
                            IOT.hideOverlay();
                            if (ret && ret.code === 200) {
                                IOT.tips('保存成功！', 'success');
                                M.Table.refresh.all();
                                M.Page.emitPrePage();
                            }
                            else {
                                IOT.tips(ret.message || '操作失败，请重试！', 'error');
                            }
                        });
                        return false;
                    }
                });
            });
        }
    });
})();